var http   = new XMLHttpRequest();
var https  = new XMLHttpRequest();
var apikey = 'eOH9MFXdhvd2Ogx4oQ5MQuYiFpNOkl8b';
// var ajax;
/*
function setAjax(obj) {
  ajax = obj;
}
*/
function download(callback) {

  var xml = new XMLHttpRequest();
  xml.onreadystatechange = function() {
    if (xml.readyState == 4) {
      var html = xml.responseText;
      var value;
      if (value = html.match(/<tr id=[^\>]+>/g)) {
        console.log(value.length);
      }
    }
  }
  xml.open('GET', 'http://forum12.hkgolden.com/topics.aspx?type=BW', true);
  xml.send(null);
}

function oauth() {
  var params = 'oauth_consumer_key=B2lxkOMDnfCH&oauth_consumer_secret=uu0yrNkFTIa98QJnZMLt5qdWLrsqjvM5&oauth_signature_method=\'HMAC-SHA1\'&oauth_nonce=483290&oauth_timestamp=1311401312';
  ajax.post('http://www.plurk.com/OAuth/request_token', params, false);
}

function login(user, password, callback) {
//  var params = 'api_key=' + apikey + '&username=' + user + '&password=' + password + '&no_data=1';
/*
  https.open('POST', 'https://www.plurk.com/API/Users/login', true);
  https.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  https.setRequestHeader('Content-Length', params.length);
  https.send(params);
  https.onreadystatechange = function() {
    if (https.readyState == XMLHttpRequest.HEADERS_RECEIVED &&
        https.status == 400) {
      callback(false);
    } else if (https.readyState == XMLHttpRequest.DONE) {
      if (https.status == 200) {
        console.log(" Plurk Login " + https.getAllResponseHeaders());
        var result = eval('(' + https.responseText + ')');
        callback(result.success_text == "ok");
      }
    }
  }
*/
        console.log(" Password Login " + params);
  ajax.post('http://www.plurk.com/API/Users/login', params, false);
}

function poll(model, count, unread, callback) {
  var params =  'api_key=' + apikey + '&limit=' + count;
  ajax.post("http://www.plurk.com/API/Timeline/getPlurks", params, true);
}

function poll_unread(model, count, unread, callback) {
  var params =  'api_key=' + apikey + '&limit=' + count;
  ajax.post("http://www.plurk.com/API/Timeline/getUnreadPlurks", params, true);
}


function response(model, id, count, callback) {
  var params = 'api_key=' + apikey + '&plurk_id=' + id + '&from_response=' + count;

  ajax.post("http://www.plurk.com/API/Responses/get", params, true);
}

function handlePlurk(model, users, _plurk, selection) {
  var profile, name, avatar, imagelink, name, response, data, plurk_id, is_unread, posted;
  for (var count = 0; count < _plurk.length; count++) {
    var user_id = 0;
    if (selection == 0) {
      user_id = _plurk[count].owner_id;
    } else if (selection == 1) {
      user_id = _plurk[count].user_id;
    }
    profile = users[user_id].has_profile_image;
    avatar = users[user_id].avatar;

    name = users[user_id].display_name;
    if (typeof(name) == "undefined")
      name = users[user_id].nick_name;
    if (typeof(name) == "undefined")
      name = users[user_id].full_name;

    if (profile == 1 && (avatar == 0 || avatar == null)) {
      imagelink = "http://avatars.plurk.com/" + user_id + "-medium.gif";
    } else if (profile == 1 && avatar != 0) {
      imagelink = "http://avatars.plurk.com/" + user_id + "-medium" + avatar + ".gif";
    }  else if (profile == 0) {
      imagelink = "http://www.plurk.com/static/default_medium.gif";
    }

    response = _plurk[count].response_count;
    data = _plurk[count].content;
    plurk_id = _plurk[count].plurk_id;
    is_unread = _plurk[count].is_unread;
    posted = _plurk[count].posted;

//    console.log("Plurk " + plurk[count].limited_to);

    var plurkObject = {
      "plurk_id": plurk_id,
      "plurk_name": name,
      "plurk_data": data,
      "posted": posted,
      "avatar": imagelink,
      "response": response,
      "is_unread": is_unread,
      "plurk_private": _plurk[count].limited_to,
      "posted": posted,
    };
    console.log("POST " + posted);

    model.append(plurkObject);
//    console.log("Count " + count + " " + data);
  }
}
